package com.jsgt.greekfire.ui.home.menu.powerise

import android.view.View
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.fhx.core.base.BaseActivity
import com.fhx.core.goToActivity
import com.jsgt.greekfire.R
import com.jsgt.greekfire.bean.PrivilegeBean
import com.jsgt.greekfire.viewmodel.PrivilegeViewModel
import kotlinx.android.synthetic.main.activity_privilege.*
import kotlinx.android.synthetic.main.view_title.*
import org.jetbrains.anko.backgroundResource
import org.jetbrains.anko.imageResource
import org.jetbrains.anko.textColorResource

class PrivilegeActivity : BaseActivity(), View.OnClickListener {
    private val privilegeViewModel: PrivilegeViewModel by lazy {
        ViewModelProvider(this).get(PrivilegeViewModel::class.java)
    }
    private val listAdapter: PrivilegeAdapter by lazy {
        PrivilegeAdapter()
    }
    private var privilegeType = 0 //0-云创智  1-云创联

    companion object{
        const val TYPE_STR = "type"
    }

    override fun needFitsSystemWindows(): Boolean {
        return false
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_privilege
    }

    override fun main() {
        privilegeType = intent.getIntExtra(TYPE_STR, 0)
        setStatusBar(R.color.transparent, false, R.color.white)
        title_layout.backgroundResource = R.color.transparent
        back.imageResource = R.mipmap.ic_back1
        title_tv.textColorResource = R.color.privilege_text_title
        title_tv.text = if (privilegeType == 0) {
            getString(R.string.powerise)
        } else {
            getString(R.string.cl)
        }
        txv.text = title_tv.text
//        right_tv.textColorResource = R.color.privilege_text_title
//        right_tv.visibility = View.VISIBLE
//        right_tv.text = if (type == 0) {
//            getString(R.string.powerise_income)
//        } else {
//            getString(R.string.cl_income)
//        }
//        right_tv.setOnClickListener(this)
        txvWallet.text = if (privilegeType == 0) {
            getString(R.string.powerise_wallet)
        } else {
            getString(R.string.cl_wallet)
        }
        txvWallet.setOnClickListener(this)

        recycler.layoutManager = LinearLayoutManager(this)
        recycler.adapter = listAdapter

        privilegeViewModel.getPrivilege(privilegeType)
        privilegeViewModel.privilegeData.observe(this, Observer {
            listAdapter.setNewInstance(it as MutableList<PrivilegeBean>)
        })
        privilegeViewModel.errorLiveData.observe(this, Observer {
            showToast(it.toString())
        })
    }

    override fun onClick(v: View?) {
        when (v?.id) {
//            R.id.right_tv -> {
//                showToast(getString(R.string.not_yet_open))
//            }
            R.id.txvWallet -> {
                goToActivity<PrivilegeWalletActivity> {
                    putExtra("type", privilegeType)
                }
            }
        }
    }
}